﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace Alfa.Common.ValidationRules {
	public class TimeValidationRule : ValidationRuleBase {
		public TimeValidationRule( ) { }

		public bool Nullable { get; set; }

		public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) {
			if (value is String) {
				TimeSpan timeSpan;
				DateTime dateTime;

				if (((string)value).Length > 0) {
					if (TimeSpan.TryParse((string)value, out timeSpan))
						return this.ParseError(true, null);
					else if (DateTime.TryParse((string)value, out dateTime))
						return this.ParseError(true, null);
					else
						return this.ParseError(false, Messages.NotATime);
				} else if (((string)value).Length == 0 && this.Nullable) {
					return this.ParseError(true, null);
				} else {
					return this.ParseError(false, Messages.NotATime);
				}
			} else if (this.Nullable && value.Equals(null)) {
				return this.ParseError(true, null);
			} else if (value is DateTime) { // Does value is already DateTime?
				return this.ParseError(true, null);
			} else if (value is TimeSpan) { // Does value is already TimeSpan?
				return this.ParseError(true, null);
			}

			return this.ParseError(false, Messages.NotATime);
		}
	}
}
